package top.wshape1.shiziapi.project.controller;

import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.wshape1.shiziapi.common.constant.AuthConstant;
import top.wshape1.shiziapi.common.domain.CommonUser;
import top.wshape1.shiziapi.common.utils.Base64Util;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.project.domain.ApiTag;
import top.wshape1.shiziapi.project.domain.vo.*;
import top.wshape1.shiziapi.project.service.ApiService;

import java.util.List;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-11-07
 */

@RestController
@RequestMapping("/api")
public class ApiController {

    @Resource
    private ApiService apiService;

    /**
     * 新增tag
     *
     * @param createTagVo
     * @param userBase64
     * @return
     */
    @PostMapping("/tag")
    public R<String> saveTag(@RequestBody @Validated CreateTagVo createTagVo,
                             @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        Long tagId = apiService.createTag(createTagVo, commonUser.getId());

        return R.ok().putData(tagId.toString());
    }

    /**
     * 移除tag
     *
     * @param twoIdVo
     * @param userBase64
     * @return
     */
    @PostMapping("/tag/remove")
    public R<Void> removeTag(@RequestBody @Validated TwoIdVo twoIdVo,
                             @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        apiService.removeTag(twoIdVo, commonUser.getId());

        return R.ok();
    }

    /**
     * 获取tagList
     *
     * @param twoIdVo
     * @param userBase64
     * @return
     */
    @PostMapping("/tag/list")
    public R<List<ApiTag>> listTag(@RequestBody @Validated TwoIdVo twoIdVo,
                                   @RequestHeader(value = AuthConstant.HEADER_PAYLOAD_NAME, required = false) String userBase64) {

        Long userId = null;
        if (userBase64 != null)
            userId = Base64Util.decodeAndParse(userBase64, CommonUser.class).getId();

        List<ApiTag> tags = apiService.listTag(twoIdVo, userId);

        return R.ok(tags);
    }

    /**
     * 为某个文档的添加一个内容
     *
     * @param createContentVo
     * @param userBase64
     * @return
     */
    @PostMapping("/content/add")
    public R<String> addContent(@RequestBody @Validated CreateContentVo createContentVo,
                                @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        Long id = apiService.createContent(createContentVo, commonUser.getId());


        return R.ok().putData(id.toString());
    }


    /**
     * 删除一个内容
     *
     * @param removeContentVo
     * @param userBase64
     * @return
     */
    @PostMapping("/content/remove")
    public R<String> removeContent(@RequestBody @Validated RemoveContentVo removeContentVo,
                                   @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        apiService.removeContent(removeContentVo, commonUser.getId());

        return R.ok();
    }

    /**
     * 重命名一个内容
     *
     * @param renameContentVo
     * @param userBase64
     * @return
     */
    @PostMapping("/content/rename")
    public R<String> removeContent(@RequestBody @Validated RenameContentVo renameContentVo,
                                   @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        apiService.renameContent(renameContentVo, commonUser.getId());

        return R.ok();
    }

    /**
     * 获取文档的整体数据
     *
     * @param twoIdVo
     * @param userBase64
     * @return
     */
    @PostMapping("/data")
    public R<ApiDataVo> getApiData(@RequestBody @Validated TwoIdVo twoIdVo,
                                   @RequestHeader(value = AuthConstant.HEADER_PAYLOAD_NAME, required = false) String userBase64) {

        Long userId = null;
        if (userBase64 != null)
            userId = Base64Util.decodeAndParse(userBase64, CommonUser.class).getId();

        ApiDataVo apiData = apiService.getApiData(twoIdVo, userId);

        return R.ok(apiData);
    }

    /**
     * 保存文档
     *
     * @param apiDataVo
     * @param userBase64
     * @return
     */
    @PostMapping("/save")
    public R<Void> saveApiData(@RequestBody @Validated ApiDataVo apiDataVo,
                                    @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        apiService.saveApiData(apiDataVo, commonUser.getId());

        return R.ok();
    }


}
